﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using EHSubmit.Model;
using EHSubmit.Util;
using EHSubmit.Logic.LINQ;

/// <summary>
///BaseCourseManager 的摘要说明
/// </summary>
public class BaseCourseManager
{
    private const string SQL_SELECT_BaseCourse = "SELECT BaseCourseID,CourseName FROM BaseCourse WHERE TID = @TID";
    private const string SQL_INSERT_BaseCourse = "INSERT INTO BaseCourse(TID,BaseCourseID,CourseName) VALUES (@TID,@BaseCourseID,@CourseName)";

    private const string PARM_TID = "@TID";                           /*教师ID*/
    private const string PARM_BaseCourseID = "@BaseCourseID";         /*课程ID，注：非课程班级ID*/
    private const string PARM_CourseName = "@CourseName";             /*课程名称*/

    public BaseCourseManager()
    {
    }

    /// <summary>
    /// 列出老师曾教过的课程，用于查找老师曾在某类课程布置过的习题
    /// </summary>
    /// <param name="teacherID">老师ID</param>
    /// <returns>老师曾教过的课程</returns>
    public IList<BaseCourse> GetBaseCourseList(string teacherID)
    {
        try
        {
            IList<BaseCourse> baseCourseList = new List<BaseCourse>();
            #region 原来实现
            //SqlParameter parm = new SqlParameter(PARM_TID, SqlDbType.NVarChar, 13);
            //using (SqlDataReader rdrBaseCourse = (SqlDataReader)SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_BaseCourse, parm))
            //{
            //    while (rdrBaseCourse.Read())
            //    {
            //        BaseCourse baseCourse = new BaseCourse();
            //        baseCourse.ID = rdrBaseCourse["BaseCourseID"] is DBNull ? string.Empty : (string)rdrBaseCourse["BaseCourseID"];                /*教师ID*/
            //        baseCourse.Name = rdrBaseCourse["CourseName"] is DBNull ? string.Empty : (string)rdrBaseCourse["CourseName"];                  /*课程ID，注：非课程班级ID*/

            //        baseCourseList.Add(baseCourse);
            //    }
            //}
            //return baseCourseList;
            #endregion
            using (EHSubmitDataContext eDataContext = new EHSubmitDataContext(SqlHelper.ConnectionStringLocalTransaction))
            {
                baseCourseList = (from bc in eDataContext.TeacherBaseCourse
                                  where bc.TID == teacherID
                                  select new BaseCourse() { ID = bc.BaseCourseID, Name = bc.CourseName }).ToList<BaseCourse>();
            }
            return baseCourseList;

        }
        catch (Exception ex)
        {
            LogHelper.Log(ex.Message, ex);
            return null;
        }
    }
}

